/*
 * @(#)InviteesPickerManager.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import com.dcivision.staff.dao.StaffHierarchyDAObject;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
 InviteesPickerManager.java
 
 @author      Jim Zhou
 @company     DCIVision Limited
 @creation date   2006/11/24
 @version     $Revision: 1.3.4.1 $
 */
public class InviteesPickerManager implements InviteePickerManager {
  
  protected Log log = new LogFactoryImpl().getInstance(this.getClass());
  public static final String E_MAIL = "E_MAIL";
  public static final String FULL_NAME = "FULL_NAME";
  public static final String ID = "ID";
  public static final String RECORD_TYPE = "RECORD_TYPE";
  public static final String TYPE_GROUP = "G";
  public static final String TYPE_ROLE = "R";
  public static final String TYPE_HIERARCHY = "S";
  public static final String TITLE = "TITLE";
  public static final String LVL1_NAME = "LVL1_NAME";
  public static final String LVL2_NAME = "LVL2_NAME";
  public static final String LVL3_NAME = "LVL3_NAME";
  public static final String LVL4_NAME = "LVL4_NAME";
  public static final String LVL5_NAME = "LVL5_NAME";
  public static final String LVL6_NAME = "LVL6_NAME";
  public static final String LVL7_NAME = "LVL7_NAME";
  public static final String LVL8_NAME = "LVL8_NAME";
  public static final String LVL9_NAME = "LVL9_NAME";
  public static final String LVL10_NAME = "LVL10_NAME";

  public static final String GROUP_VIEW = "GROUP_VIEW";
  public static final String ROLE_VIEW = "ROLE_VIEW";
  
  private Connection conn;
  
  public InviteesPickerManager() {
    super();
  }
  
  /**
   * get list with column'name.
   * @param                  String columnName
   * @param                  SessionContainer sessionContainer
   * @return                 List
   */
  public List getResultList(String columnName, SessionContainer sessionContainer) {
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserRecordDAObject userRecordDAObject = new UserRecordDAObject(sessionContainer,this.conn);
    try {
      result = userRecordDAObject.getResultList(columnName);
    } finally {
      //this.conn = null;
      this.closeConnection();
      userRecordDAObject = null; 
    }
    return result;
  }
  
  /**
   * get user/group/role list,contains name,type("U","G","R"),id,email.
   * @param                  SessionContainer sessionContainer
   * @return                 List
   */
  public List getResultList(SessionContainer sessionContainer) {
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserRecordDAObject userRecordDAObject = new UserRecordDAObject(sessionContainer,this.conn);
    try {
      result = userRecordDAObject.getResultList();
    } finally {
      //this.conn = null;
      this.closeConnection();
      userRecordDAObject = null;
    }
    return result;
  }
  
  /**
   * get role list ,contains name,id.
   * @param                  SessionContainer sessionContainer
   * @return                 List
   */
  public List getRoleList(SessionContainer sessionContainer){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserRoleDAObject userRoleDAObject = new UserRoleDAObject(sessionContainer,this.conn);
    try{
      result = userRoleDAObject.getRoleList();
    } finally {
      //this.conn = null;
      this.closeConnection();
      userRoleDAObject = null;
    }
    return result;
  }
  
  /**
   * get staff hierarchy list ,contains name,id.
   * @param                  SessionContainer sessionContainer
   * @return                 List
   */
  public List getStaffHierarchyList(SessionContainer sessionContainer){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    StaffHierarchyDAObject staffHierarchyDAObject = new StaffHierarchyDAObject(sessionContainer,this.conn);
    try{
      result = staffHierarchyDAObject.getStaffHierarchyList();
    } finally {
      //this.conn = null;
      this.closeConnection();
      staffHierarchyDAObject = null;
    }
    return result;
  }
  
  /**
   * get group list ,contains name,id.
   * @param                  SessionContainer sessionContainer
   * @return                 List
   */
  public List getGroupList(SessionContainer sessionContainer){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserGroupDAObject userGroupDAObject = new UserGroupDAObject(sessionContainer,this.conn);
    try{
      result = userGroupDAObject.getGroupList();
    } finally {
      //this.conn = null;
      this.closeConnection();
      userGroupDAObject = null;
    }
    return result;
  }
  
  /**
   * get user's records ,contains id,full_name,title and hierarchy. 
   * The page infomation contains type("G","R","S"),searchKey,searchHierarchy,offset,pageSize,order,and "ASC" or "DESC"
   * @param                  String searchType
   * @param                  String searchKey
   * @param                  String searchHierarchy
   * @param                  String pageSize
   * @param                  String orderBy
   * @param                  String orderBy
   * @param                  String req
   * @param                  SessionContainer sessionContainer
   * @return                 List
   */
  public List getUserRecordsAndPageInfo(
      String searchUserName ,
      String searchType , 
      String searchKey , 
      String searchHierarchy , 
      String startOffset , 
      String pageSize , 
      String orderBy ,
      String req ,
      SessionContainer sessionContainer){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserRecordDAObject userRecordDAObject = new UserRecordDAObject(sessionContainer,this.conn);
    try {    
      result = userRecordDAObject.getUserRecordsAndPageInfo(searchUserName,searchType,searchKey,searchHierarchy,startOffset,pageSize,orderBy,req);
    } finally {
      userRecordDAObject = null;
      //this.conn = null;
      this.closeConnection();
    }
    return result;
  }
  
  public List getResultListDynamic(
      boolean enableUser,
      boolean enableGroup,
      boolean enableRole,
      Integer choices,
      String key,
      ArrayList expectUserIDs,
      ArrayList expectGroupIDs,
      ArrayList expectRoleIDs,
      SessionContainer sessionContainer){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserRecordDAObject userRecordDAObject = new UserRecordDAObject(sessionContainer,this.conn);
    try {
      result = userRecordDAObject.getResultListDynamic(
          enableUser,
          enableGroup,
          enableRole,
          choices,
          key,
          expectUserIDs,
          expectGroupIDs,
          expectRoleIDs);
    } finally {
      userRecordDAObject = null;
      this.closeConnection();
    }
    return result;
  }
  
  public List getGroupRecordsAndPageInfo (
      String searchGroupName ,
      String startOffset ,
      String pageSize ,
      String orderBy ,
      String req ,
      SessionContainer sessionContainer
  ){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserGroupDAObject userGroupDAObject = new UserGroupDAObject(sessionContainer,this.conn);
    try {
      result = userGroupDAObject.getGroupRecordsAndPageInfo (searchGroupName, startOffset, pageSize, orderBy, req);
    } finally {
      this.closeConnection();
      userGroupDAObject = null;
    }
    return result;
  }
  
  public List getRoleRecordsAndPageInfo (
      String searchRoleName ,
      String startOffset ,
      String pageSize ,
      String orderBy ,
      String req ,
      SessionContainer sessionContainer
  ){
    this.conn = this.getDBConnection();
    List result = new ArrayList();
    UserRoleDAObject userRoleDAObject = new UserRoleDAObject(sessionContainer,this.conn);
    try {
      result = userRoleDAObject.getRoleRecordsAndPageInfo (searchRoleName, startOffset, pageSize, orderBy, req);
    } finally {
      this.closeConnection();
      userRoleDAObject = null;
    }
    return result;
  }
  
  private Connection getDBConnection(){
    if(this.conn!=null){
      return conn;
    }else{
      try{
        conn=DataSourceFactory.getConnection();
      }catch(Exception e){
        log.error("get Connection error!");
      }
      return conn;
    }
  }
  
  private void closeConnection() {
    if(this.conn != null ){
      try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
      log.info("Close Connection Succeed");
  }
}
  
}
